package model.service;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;
import model.OnScreen;
import model.Snippet;

public class JSONBehaviour extends ServiceBehaviour {

	@Override
	public HashMap<String, Snippet> translateResponse(String response) throws Exception{

		HashMap<String, Snippet> snippets = new HashMap<String, Snippet>();
		try
		{
			Pixelator.calculateScales();
			
			JSONArray jsonArray = new JSONArray(response);	
			for(int i = 0; i < jsonArray.length(); i ++)
			{
				JSONObject jsonSnippet = jsonArray.getJSONObject(i);
				JSONObject jsonDimension = jsonSnippet.getJSONObject("Size");
				JSONObject jsonScreenCoord = jsonSnippet.getJSONObject("Coord");
				
				snippets.put(
						jsonSnippet.getString("Id"), 
						createSnippet(
								jsonSnippet.getString("Id"), 
								jsonScreenCoord.getInt("X"), jsonScreenCoord.getInt("Y"), jsonScreenCoord.getInt("Z"), 
								jsonDimension.getInt("Width"), jsonDimension.getInt("Height"), 
								jsonSnippet.getBoolean("Value"), jsonSnippet.getInt("Display")
							)
						);
			}
		
			for (Snippet snippet : snippets.values()) 
			{
				Log.d("SNIPPET", snippet.toString());
			}
		}
		catch(Exception e)
		{
			OnScreen.getInstance().notifyScreenWithToast("Datatransfer problem with service"); 
		}
		return snippets;
	}
}
